package ta.dao;

import ta.pojo.User;
import ta.utils.JDBCUtils;
import ta.utils.PageInfo;

import java.util.ArrayList;
import java.util.List;

public class UserDao {
    //用户登录
    public boolean login(User user) {
        String sql="select * from user where username=? and userpwd=?";
        Object[] params= {user.getUsername(),user.getUserpwd()};
        return JDBCUtils.getList(sql, params,User.class).size()==1;
    }
    //用户注册 增加用户
    public boolean  register(User user) {
        String sql="insert into  user(username,userpwd) values(?,?) ";
        Object[] params= {user.getUsername(),user.getUserpwd()};
        return JDBCUtils.update(sql, params);
    }

    public boolean selectByUsername(User user) {
        String sql="select * from user where username=?";
        Object[] params= {user.getUsername()};
        return JDBCUtils.getList(sql, params,User.class).size()==1;
    }
    public User getByName(String username) {
        String sql="select * from user where username=? ";
        Object[] params= {username};
        return JDBCUtils.getBean(sql,params, User.class);
    }
    //删除用户
    public boolean del(String ids) {
        String[] params=ids.split(",");
        String sql="delete from user where userid in (";
        for(String k:params)
        {
            sql+="?,";
        }
        sql=sql.substring(0,sql.length()-1);
        sql+=")";
        return JDBCUtils.update(sql, params);
    }
    //修改用户
    public boolean  edit(User user) {
        List<Object> list=new ArrayList<Object>();
        StringBuilder sql=new StringBuilder("update user  set ");
        if(user.getUsername()!=null)
        {
            sql.append("username=?,");
            list.add(user.getUsername());
        }
        if(user.getUserpwd()!=null)
        {
            sql.append("userpwd=?,");
            list.add(user.getUserpwd());
        }
        sql.deleteCharAt(sql.length()-1);
        sql.append("  where userid=?");
        list.add(user.getUserid());
        return JDBCUtils.update(sql.toString(), list.toArray());
    }

    public<T> void getAll(String keyword, PageInfo<User> pb) {
        String sql1="select count(*) from user where  userid like  concat('%',?,'%') or username like concat('%',?,'%') ";
        Object[] params1= {keyword,keyword};
        long total=(long) JDBCUtils.getScalar(sql1, params1);
        int totalpage=(int)(total+pb.getPagesize()-1)/pb.getPagesize();
        String sql="select * from user where  userid like  concat('%',?,'%') or username like concat('%',?,'%')  "
                + " limit ?,?";
        Object[] params= {keyword,keyword,(pb.getCurrentpage()-1)*pb.getPagesize(),pb.getPagesize()};
        pb.setTotalpage(totalpage);
        pb.setData(JDBCUtils.getList(sql, params,User.class));
    }

    public User getById(String userid) {
        String sql="select * from user where userid=? ";
        Object[] params= {userid};
        return JDBCUtils.getBean(sql,params, User.class);
    }

    public static void main(String[] args) {
        UserDao dao=new UserDao();
        User user=new User();
        user.setUsername("ccc");
        user.setUserpwd("ccc");
        dao.register(user);
    }

}
